<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Index extends AdminController
{
    public function index()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();

        return view('login/login', [
            'title' => '登陆页','list'=>$list
        ]);
    }

    

    public function main()
    {
        $list = db('ad_user')->field(['id','name','status'])->order('id','ASC')->select();

        return view('main/index', [
            'title' => '登陆页','list'=>$list,'time'=>date('Y/m/d',time())
        ]);
    }



    //确认登陆
    public function dologin(Request $request)
    {
        $p = $request->post();
        $data = [
            'name' => $p['name'],
            'pwd' => ($p['pwd']),
        ];

        $res = db('ad_user')
            ->where(['name'=>$data['name'],'pwd'=>$data['pwd']])
            ->find();
       // var_dump($res);die;
        if ($res){
//            return $this->success('登陆成功', 'login/index/show');
            return view('main/index',[
                'title'=>$data['name']
            ]);
        }else {
            return $this->error('登陆失败');
        }
    }

    public function edit($id)
    {
        $list = Db::table('mh_ad_user')->where('id',$id)->select();
        return view('main/edit',[
            'list'=>$list[0],
        ]);
    }

    public function create()
    {
        return view('main/add',['creatime' =>date('Y/m/d',time())]);
        
        
        $name = $_POST['name'];
        $pass = $_POST['pwd'];
        $status = $_POST['status'];
        
        $data = [
            'name' => $name,
            'pwd' => $pass,
            'status' => $status,
            'create' => date('Y/m/d',time())
        ];
        $result = db('ad_user')->insert($data);
        if($result){
        $this->success('添加成功');
    }
}

    public function save(Request $request)
    {
        $result = db('ad_user')->insert($_POST);
        // dump($result);
    }

    public function update(Request $request, $id)
    {

        $id = $_POST['id'];
        $name = $_POST['name'];
        $pass = $_POST['pwd'];
        $status = $_POST['status'];
        $result = db('ad_user')->where('id', $id)->update(['name' => $name,'pwd' =>$pass,'status' => $status]);
        
        if($result){
        $this->success('修改成功','admin/index/main');
     }
    }

    public function delete($id)
    {
        $result = Db::name('ad_user')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为' . $id . '的用户删除失败!';
        }
        // JSON 返回
        return json($info);
    
    }


    // public function rolelist(){
    //         //查询节点信息
    //         $list = $this->_role->where('status=1')->select();
    //         //查询当前用户信息
    //         $users = $this->_user->where(array('id'=>array('eq',I('id'))))->find();

    //         //获取当前用户的角色信息
    //         $rolelist = $this->_user_role->where(array('uid'=>array('eq',I('id'))))->select();

    //         $myrole = array(); //定义空数组
    //         //对用户的角色进行重组
    //         foreach($rolelist as $v){
    //             $myrole[] = $v['rid'];
    //         }
            

    //         //分配数据
    //         $this->assign("list",$list);
    //         //分配当前用户信息
    //         $this->assign('users',$users);
    //         //分配该用户的角色信息
    //         $this->assign('role',$myrole);

    //         //加载模板
    //         $this->display();
    //     }

    //     //保存用户角色
    //     public function saverole(){
            
    //         //判读必须选择一个角色
    //         if(empty($_POST['role'])){
    //             $this->error("请选择一个角色！");
    //         }

    //         $uid = $_POST['uid'];

    //         //清除用户所有的角色信息，避免重复添加
    //         $this->_user_role->where(array('uid'=>array('eq',$uid)))->delete();
    
    //         foreach(I('role') as $v){
    //             $data['uid'] = $uid;
    //             $data['rid'] = $v;
    //             //执行添加
    //             $this->_user_role->data($data)->add();

    //         }

    //         $this->success("角色分配成功",U('User/index'));
            
    //     }

    //     public function rolecreate(){
    //         $data = db('ad_role')->field(['id','name'])->order('id','ASC')->select();
    //         return view('main/addrole',['data'=>$data]);
    //     }

        public function editrole($id)
    {
        // $guanli = Session::get('adname');

        //查找该用户角色id
        $role_id = db('ad_user_role')->where('user_id',$id)->field('role_id')->select();

        // var_dump($role_id);
        //查找所有的角色
        $data = db('ad_role')->select();

        $roid = array();
       //遍历重组数组
        foreach($role_id as $v){
            $roid[] = $v['role_id'];
        }
        // var_dump($roid);die;
//        dump($ro_nodes);die;
        return view('main/addrole',[
            'roid'=>$roid,
            // 'guanli' => $guanli,
            'data' => $data,
            'role_id' => $role_id,
            'uid' => $id
        ]);

    }

    public function rolesave(Request $request)
    {
        $arr = array();
        $p = $request->post();
//        dump($p);die;
        $data = $p['a'];
//        dump($data);die;
        $user_id = $p['uid'];

        $qu = db('ad_user_role')->field('user_id')->select();
//        dump($qu);die;
        foreach ($qu as $k=>$v){
           if (in_array($user_id,$v)){
               $res = db('ad_user_role')->where('user_id',$user_id)->delete();
           }
        }

        foreach($data as $v){
            $arr['user_id'] = $user_id;
            $arr['role_id'] = $v;
            $rev = db('ad_user_role')->data($arr)->insert();
            $arr = [];
        }
        if ($rev > 0){
            return $this->success('编辑成功', url('admin/index/index'));
        }else{
            return $this->error('编辑失败');
        }
    }



}
